package Pages.System;

import com.jgoodies.forms.factories.DefaultComponentFactory;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import static Data.Util.checkByUnameAndPwd;
import static Data.Util.getCount;

/**
 * description:
 * author: 柳胜酉
 * 2022/7/8
 */
public class LoginPage extends JFrame {
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JPanel panel7;
    private JPanel panel1;
    private JLabel title1;
    private JPanel panel8;
    private JLabel label4;
    private JTextField textField1;
    private JPanel panel9;
    private JLabel label5;
    private JPasswordField passwordField1;
    private JPanel panel10;
    private JButton button1;
    private JButton button2;
    private JPanel panel2;
    private JRadioButton radioButton7;
    private JRadioButton radioButton8;
    private JRadioButton radioButton9;

    private String table = null;
    private String count = null;

    public LoginPage() {
        initComponents();
    }

    private void initComponents() {
        ResourceBundle bundle = ResourceBundle.getBundle("P0704");
        DefaultComponentFactory compFactory = DefaultComponentFactory.getInstance();
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        panel7 = new JPanel();
        panel1 = new JPanel();
        title1 = compFactory.createTitle(bundle.getString("test01.title1.textWithMnemonic"));
        panel8 = new JPanel();
        label4 = new JLabel();
        textField1 = new JTextField();
        panel9 = new JPanel();
        label5 = new JLabel();
        passwordField1 = new JPasswordField();
        panel10 = new JPanel();
        button1 = new JButton();
        button2 = new JButton();
        panel2 = new JPanel();
        radioButton7 = new JRadioButton();
        radioButton8 = new JRadioButton();
        radioButton9 = new JRadioButton();

        //======== this ========
        setTitle(bundle.getString("test01.this.title"));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setBackground(SystemColor.inactiveCaption);
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setBackground(SystemColor.inactiveCaption);
                contentPanel.setLayout(new GridLayout(5, 2));

                //======== panel7 ========
                {
                    panel7.setLayout(new GridLayout(1, 1));

                    //======== panel1 ========
                    {
                        panel1.setBackground(SystemColor.activeCaption);
                        panel1.setLayout(new GridLayout(1, 1));

                        //---- title1 ----
                        title1.setHorizontalAlignment(SwingConstants.CENTER);
                        title1.setFont(title1.getFont().deriveFont(title1.getFont().getSize() + 20f));
                        title1.setForeground(Color.white);
                        panel1.add(title1);
                    }
                    panel7.add(panel1);
                }
                contentPanel.add(panel7);

                //======== panel8 ========
                {
                    panel8.setBackground(SystemColor.inactiveCaption);
                    panel8.setLayout(new GridLayout(1, 2));

                    //---- label4 ----
                    label4.setText(bundle.getString("test01.label4.text"));
                    label4.setHorizontalAlignment(SwingConstants.CENTER);
                    label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 10f));
                    label4.setForeground(Color.darkGray);
                    label4.setIconTextGap(1);
                    label4.setIcon(new ImageIcon(getClass().getResource("/image/denglu2.png")));
                    panel8.add(label4);

                    //---- textField1 ----
                    textField1.setHorizontalAlignment(SwingConstants.CENTER);
                    textField1.setFont(textField1.getFont().deriveFont(textField1.getFont().getSize() + 6f));
                    textField1.setBackground(Color.white);
                    textField1.setForeground(Color.black);
                    textField1.setPreferredSize(new Dimension(59, 31));
                    panel8.add(textField1);
                }
                contentPanel.add(panel8);

                //======== panel9 ========
                {
                    panel9.setBackground(SystemColor.inactiveCaption);
                    panel9.setLayout(new GridLayout(1, 2));

                    //---- label5 ----
                    label5.setText(bundle.getString("test01.label5.text"));
                    label5.setHorizontalAlignment(SwingConstants.CENTER);
                    label5.setFont(label5.getFont().deriveFont(label5.getFont().getSize() + 10f));
                    label5.setForeground(Color.darkGray);
                    label5.setIcon(new ImageIcon(getClass().getResource("/image/mima2.png")));
                    panel9.add(label5);

                    //---- passwordField1 ----
                    passwordField1.setHorizontalAlignment(SwingConstants.CENTER);
                    passwordField1.setFont(passwordField1.getFont().deriveFont(passwordField1.getFont().getSize() + 6f));
                    panel9.add(passwordField1);
                }
                contentPanel.add(panel9);

                //======== panel10 ========
                {
                    panel10.setBackground(new Color(153, 204, 255));
                    panel10.setLayout(new GridLayout(1, 2));

                    //---- button1 ----
                    button1.setText(bundle.getString("test01.button1.text"));
                    button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 10f));
                    button1.setForeground(new Color(15, 16, 21));
                    button1.setBackground(UIManager.getColor("Button.shadow"));
                    panel10.add(button1);

                    //---- button2 ----
                    button2.setText(bundle.getString("test01.button2.text"));
                    button2.setFont(button2.getFont().deriveFont(button2.getFont().getSize() + 10f));
                    button2.setForeground(new Color(15, 16, 21));
                    button2.setBackground(UIManager.getColor("Button.shadow"));
                    panel10.add(button2);
                }
                contentPanel.add(panel10);

                //======== panel2 ========
                {
                    panel2.setBackground(SystemColor.inactiveCaption);
                    panel2.setLayout(new FlowLayout());

                    //---- radioButton7 ----
                    radioButton7.setText(bundle.getString("test01.radioButton7.text"));
                    radioButton7.setBackground(SystemColor.inactiveCaption);
                    radioButton7.setFont(radioButton7.getFont().deriveFont(radioButton7.getFont().getStyle() | Font.BOLD, radioButton7.getFont().getSize() + 5f));
                    radioButton7.setForeground(Color.black);
                    panel2.add(radioButton7);

                    //---- radioButton8 ----
                    radioButton8.setText(bundle.getString("test01.radioButton8.text"));
                    radioButton8.setBackground(SystemColor.inactiveCaption);
                    radioButton8.setFont(radioButton8.getFont().deriveFont(radioButton8.getFont().getStyle() | Font.BOLD, radioButton8.getFont().getSize() + 5f));
                    radioButton8.setForeground(Color.black);
                    panel2.add(radioButton8);

                    //---- radioButton9 ----
                    radioButton9.setText(bundle.getString("test01.radioButton9.text"));
                    radioButton9.setBackground(SystemColor.inactiveCaption);
                    radioButton9.setFont(radioButton9.getFont().deriveFont(radioButton9.getFont().getStyle() | Font.BOLD, radioButton9.getFont().getSize() + 5f));
                    radioButton9.setForeground(Color.black);
                    panel2.add(radioButton9);
                }
                contentPanel.add(panel2);
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        setSize(500, 420);
        setLocationRelativeTo(null);

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radioButton7);
        buttonGroup1.add(radioButton8);
        buttonGroup1.add(radioButton9);

        radioButton7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (radioButton7.isSelected()) {
                    table = "user_info";
                }
            }
        });

        radioButton8.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (radioButton8.isSelected()) {
                    table = "user_info_t";
                }
            }
        });

        radioButton9.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (radioButton9.isSelected()) {
                    table = "user_info_m";
                }
            }
        });

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                count = getCount(textField1.getText());
                if (checkByUnameAndPwd(textField1.getText(), new String(passwordField1.getPassword()), table)) {
                    JOptionPane.showMessageDialog(null, "登录成功！", "登录提示", JOptionPane.INFORMATION_MESSAGE);
                    if (table == "user_info") {
                        new SHomePage(count).setVisible(true);
                        setVisible(false);
                    } else if (table == "user_info_t") {
                        new THomePage().setVisible(true);
                        setVisible(false);
                    } else if (table == "user_info_m") {
                        new MHomePage().setVisible(true);
                        setVisible(false);
                    }

                } else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误！", "登录提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (table == "user_info") {
                    setVisible(false);
                    new SRegisterPage().setVisible(true);
                } else if (table == "user_info_t") {
                    setVisible(false);
                    new TRegisterPage().setVisible(true);
                } else if (table == "user_info_m") {

                }
            }
        });

    }


    public static void main(String[] args) {
        new LoginPage().setVisible(true);
    }
}
